<template>
	<view class="container">
		<uni-nav-bar class="ch-nav" height="88rpx" backgroundColor="#ffffff" leftWidth="90rpx" rightWidth="90rpx" color="#333" :fixed="true" :padding="false" :border="false" @clickLeft="$goBack(1)">
			<template slot="left">
				<image class="left" src="@/public/images/public/dhl_fhjt.png" mode="aspectFit"></image>
			</template>
			<text class="title">优惠券</text>
			<template slot="right">
			</template>
		</uni-nav-bar>
		<view class="head flex-r ac" :class="navBorder ? 'navborder':''">
			<text :class="{'active': tabCur == index}" v-for="(item,index) in tabList" :key="index" @tap="tabChange(index)">
				{{item.name}}
			</text>
		</view>
		<template v-for="(item, index) in tabList">
			<keep-alive>
			<view class="body-tab" :key="index" v-if="tabCur == index" @touchstart="touchStart" @touchend="touchEnd">
				<view class="body-tab-detail" v-if="couponLists[tabData[index].status]">
					<view class="coupon-list">
						<view class="coupon flex-r" v-for="(coupon, index) in couponLists[tabData[index].status]" :key="index" :class="{'active': coupon.status==1}">
							<view class="coupon-name flex-c" :class="'type'+coupon.couponType + ' tab' + tabCur">
								<view class="title flex-r">
									<text class="value" v-if="coupon.couponType < 3">{{coupon.jzAmount}}</text>
									<text class="value small" v-if="coupon.couponType == 3">免单</text>
									<text class="value" v-if="coupon.couponType == 4">{{(coupon.xxAmount % 10 == 0) ? coupon.xxAmount/10 : coupon.xxAmount}}</text>
									<text class="value small" v-if="coupon.couponType == 4">折</text>
								</view>
								<text class="des" v-if="coupon.couponType < 3">彩虹豆</text>
							</view>
							<view class="coupon-detail flex-c">
								<text class="title">{{coupon.couponTypeTxt}}</text>
								<text class="des" v-if="coupon.couponType >= 3">不大于{{coupon.jzAmount}}彩虹豆可用</text>
								<text class="des" v-if="coupon.couponType == 2">全场通用</text>
								<text class="des" v-if="coupon.couponType == 1">满{{coupon.xxAmount}}彩虹豆可用</text>
								<text class="date">有效期至{{coupon.validEndTime.replace(/-/gi, '.')}}</text>
							</view>
							<view class="coupon-tag" :class="'active'+coupon.status" v-if="coupon.sourceFrom == 'welfare' && coupon.expertName">
								<text>{{coupon.expertName}}专用</text>
							</view>
							<!-- <view class="coupon-line"></view>
							<view class="coupon-status flex-r">
								<text v-if="tabCur==0">未使用</text>
								<text v-if="tabCur==1">已使用</text>
								<text v-if="tabCur==2">已失效</text>
							</view> -->
						</view>
					</view>
				</view>
				<EmptyContent v-else top="160rpx" text="暂无优惠券" img="qsy_zwyhq" @refresh="getCouponLists(tabCur)"></EmptyContent>
				<view class="list-end flex-r pb20" v-if="tabData[tabCur].isEnd">
					<text>已经到底啦～</text>
				</view>
			</view>
			</keep-alive>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				couponLists: [],
				tabList: [
					{name: '未使用'},{name: '已使用'},{name: '已失效'}
				],
				tabCur: 0,
				tabData: [
					{status:1, pageNo: 1, isEnd: false},
					{status:2, pageNo: 1, isEnd: false},
					{status:3, pageNo: 1, isEnd: false}
				],
				navBorder: false
			}
		},
		computed: {
			user() {
				return this.$store.state.user
			},
		},
		watch: {
			tabCur(newValue, oldValue) {
				if(this.couponLists[this.tabData[newValue].status]){
					return
				}
				this.getCouponLists(this.tabCur)
			}
		},
		onLoad() {
			this.getCouponLists(0)
		},
		onReachBottom() {
			if(!this.tabData[this.tabCur].isEnd){
				this.getCouponLists(this.tabCur)
			}
		},
		onPageScroll(e) {
			if(e.scrollTop > 10){
				this.navBorder = true
			}else{
				this.navBorder = false
			}
		},
		methods: {
			//用户优惠券列表
			getCouponLists(index) {
				let status = this.tabData[index].status
				this.$http.get({
					url: '/api/appUser/queryUserCouponByUserId',
					data: {
						pageNo: this.tabData[index].pageNo,
						pageSize: 10,
						userId: this.user.userId,
						status
					},
				}).then(data => {
					console.log(data);
					if(data.success && data.result.total){
						if(data.result.current == 1){
							this.$set(this.couponLists, status, data.result.records)
						}else{
							this.couponLists[status].push(...data.result.records)
						}
						this.tabData[index].isEnd = data.result.current >= data.result.pages
						this.tabData[index].pageNo = data.result.current + 1
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//选项卡切换
			tabChange(index) {
				this.tabCur = index;
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			touchStart(e) {
				if (e.changedTouches.length == 1) {
					this.startX = e.changedTouches[0].clientX
					this.startY = e.changedTouches[0].clientY
				}
			},
			touchEnd(e) {
				if (e.changedTouches.length == 1) {
					var endX = e.changedTouches[0].clientX;
					var endY = e.changedTouches[0].clientY;
					var xTowards = endX - this.startX;
					var yTowards = endY - this.startY;
					if (xTowards > 50 && Math.abs(yTowards) < 50) {//向右
						console.log('向右')
						if(this.tabCur > 0){
							this.tabChange(this.tabCur - 1)
						}
					}
					if (xTowards < -50 && Math.abs(yTowards) < 50) {//向左
						console.log('向左')
						let maxlen = this.tabList.length - 1;
						if(this.tabCur < maxlen){
							this.tabChange(this.tabCur + 1)
						}
					}
				}
			},
		}
	}
</script>

<style lang="scss">
	.container{
		padding: 0;
		font-family: PingFang SC;
	}
	.head{
		box-sizing: border-box;
		height: 88rpx;
		width: 100%;
		max-width: $max-width;
		margin: $pc-margin;
		background: #FFFFFF;
		position: fixed;
		top: 88rpx;
		z-index: 2;
		text{
			flex: 2;
			text-align: center;
			font-size: 28rpx;
			line-height: 88rpx;
			color: #999999;
		}
		.active{
			color: #333333;
		}
	}
	.body-tab{
		padding: 108rpx 20rpx var(--safe-area-inset-bottom);
		.coupon-list{
			.coupon{
				height: 200rpx;
				align-items: center;
				margin-bottom: 20rpx;
				background: #FFFFFF;
				border-radius: 10rpx;
				position: relative;
				.coupon-name{
					display: flex;
					align-items: center;
					justify-content: center;
					width: 200rpx;
					height: 200rpx;
					.title{
						align-items: center;
						.value{
							font-size: 72rpx;
							line-height: 1;
							font-weight: 800;
							&.small{
								font-size: 58rpx;
							}
						}
					}
					.des{
						text-align: center;
						margin-top: 10rpx;
						font-size: 20rpx;
						line-height: 1;
					}
					background-size: 100%;
					background-repeat: no-repeat;
					&.type1{
						background-image: url("~@/public/images/user/mjq_ysy.png");
						&.tab1, &.tab2{
							color: #FF999E !important;
						}
					}
					&.type2{
						background-image: url("~@/public/images/user/wmkq_ysy.png");
						&.tab1, &.tab2{
							color: #6CD9A3 !important;
						}
					}
					&.type3{
						background-image: url("~@/public/images/user/mdq_ysy.png");
						&.tab1, &.tab2{
							color: #80AAFF !important;
						}
					}
					&.type4{
						background-image: url("~@/public/images/user/zkq_ysy.png");
						&.tab1, &.tab2{
							color: #FFBA1A !important;
						}
					}
					
				}
				.coupon-detail{
					flex: 2;
					justify-content: center;
					margin-left: 30rpx;
					.title{
						font-size: 32rpx;
						font-weight: 500;
						line-height: 32rpx;
					}
					.des{
						margin-top: 18rpx;
						font-size: 22rpx;
						line-height: 1;
					}
					.date{
						margin: 38rpx 0 0;
						font-size: 22rpx;
						line-height: 22rpx;
						color: #999999;
					}
				}
				.coupon-tag{
					padding: 0 20rpx;
					position: absolute;
					top: 0;
					right: 0;
					height: 48rpx;
					border-radius: 0rpx 10rpx 0rpx 10rpx;
					font-size: 20rpx;
					font-weight: 500;
					line-height: 48rpx;
					&.active1{
						background: #FFDFB2;
						color: #984F00;
					}
					&.active2{
						background: #FFEFD9;
						color: #CCA57A;
					}
					&.active3{
						background: #EDEDED;
						color: #B3B3B3
					}
				}
				.coupon-line{
					width: 2rpx;
					height: 177rpx;
					background-image: url("~@/public/images/user/x.png");
					background-size: 100%;
					background-repeat: no-repeat;
				}
				.coupon-status{
					width: 88rpx;
					font-size: 28rpx;
					line-height: 28rpx;
					color: #B3B3B3;
					justify-content: center;
					align-items: center;
					text{
						writing-mode:vertical-lr;
						letter-spacing: 4rpx;
					}
				}
				&.active{
					.coupon-name{
						color: #FFFFFF !important;
						&.type1{
							background-image: url("~@/public/images/user/mjq_wsy.png");
						}
						&.type2{
							background-image: url("~@/public/images/user/wmkq_wsy.png");
						}
						&.type3{
							background-image: url("~@/public/images/user/mdq_wsy.png");
						}
						&.type4{
							background-image: url("~@/public/images/user/zkq_wsy.png");
						}
					}
					.coupon-status{
						color: #0055FF;
					}
				}
			}
		}
		.coupon-empty{
			margin-top: 160rpx;
			align-items: center;
			image{
				height: 320rpx;
			}
			text{
				margin-top: 30rpx;
				font-size: 26rpx;
				line-height: 26rpx;
				color: #B3B3B3;
			}
		}
	}
</style>
